import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.TreeMap;
import java.util.Set;

public class Index {

    /* ======================================================
     * MEMBERS
     * ====================================================== */
    private TreeMap<String, Integer> _index;
    
    /* ======================================================
     * CONSTRUCTORS
     * ====================================================== */
    public Index(){
	_index = new TreeMap<String, Integer>();
    }
    
    /* ======================================================
     * METHODS
     * ====================================================== */
    //public void build(TreeSet<String> content){
	public void build(Set<String> content){
	for (String term : content){
	    if (_index.containsKey(term)){
		_index.put(term, _index.get(term) + 1);
	    }
	    else {
		_index.put(term, 1);
	    }
	}
    }

	public boolean IsEmpty() {
		return (this._index.size() <= 0);
	}
    
    public TreeMap<String, Integer> getIndex(){
	return this._index;
    }
    
    public void print(String filename){
	FileWriter fstream;
	BufferedWriter out = null;
	try {
	    fstream = new FileWriter(filename);
	    out = new BufferedWriter(fstream);
	    for (String term : _index.keySet()){
		out.write(term + "#" + _index.get(term));
		out.newLine();
	    }
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	finally {
	    try {
		out.close();
	    } catch (IOException ioe) {
		ioe.printStackTrace();
	    }
	}
    }
}
